﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibNiconico.NiconicoApi.Internal;
using LibNiconico.NiconicoApi.Videos;
using LibNiconico.NiconicoApi.Utils;
using LibNiconico.NiconicoApi.User.Thumb;
using LibNiconico.NiconicoApi.Mylist;
using LibNiconico.NiconicoApi.Videos.Marquee;
using LibNiconico.NiconicoApi.Videos.Flv;
using LibNiconico.NiconicoApi.Videos.Thumb;
using LibNiconico.NiconicoApi.Videos.ThumbInfo;
using LibNiconico.NiconicoApi.Videos.Constant;
using System.Threading;



namespace LibNiconico.NiconicoApi.Videos
{
    /// <summary>
    /// ニコニコ動画APIヘルパー
    /// </summary>
    public sealed class NicoVideoApiHelper:NicoApiHelperBase
    {


        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="auth"></param>
        /// <param name="createInfo"></param>
        internal NicoVideoApiHelper(NiconicoAuth auth, NiconicoApiInfoGetCreateInfo createInfo) : base(auth, createInfo) { }



        /// <summary>
        /// 非同期に動画情報を取得
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public async Task<NicoVideoThumbInfoResponse> GetThumbInfoAsync(string videoId)
        {
            return await this.GetThumbInfoAsync(new NicoVideoGetThumbInfoQueryInfo(videoId)).ConfigureAwait(false);
        }

        /// <summary>
        /// 動画情報取得
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        internal async Task<NicoVideoThumbInfoResponse> GetThumbInfoAsync(NicoVideoGetThumbInfoQueryInfo query)
        {
            using (var api = new NiconicoInfoGetApiXmlImpl(NicoVideoConstant.EXT_NICOVIDEO_URL,CreateMostLoginApiHttpClient()))
            {
                return NicoVideoThumbInfoResponse.Parse( (await api.GetAsync(query).ConfigureAwait(false)).Content);
            }
        }


        /// <summary>
        /// Thumb取得
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public async Task<NicoVideoThumbResponse> GetThumbAsync(string videoId)
        {
            return await this.GetThumbAsync(new NicoVideoGetThumbQueryInfo(videoId)).ConfigureAwait(false);
        }

        /// <summary>
        /// Thumb取得
        /// </summary>
        /// <param name="queryInfo"></param>
        /// <returns></returns>
        internal async Task<NicoVideoThumbResponse> GetThumbAsync(NicoVideoGetThumbQueryInfo queryInfo)
        {
            using (var api = new NiconicoInfoGetApiXmlImpl(NicoVideoConstant.EXT_NICOVIDEO_URL, CreateMostLoginApiHttpClient()))
            {
                return NicoVideoThumbResponse.Parse( (await api.GetAsync(queryInfo).ConfigureAwait(false)).Content);
            }

        }





        /// <summary>
        /// 非同期にMarquee取得
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<NicoVideoMarqueeDataResponse> GetMarqueeAsync(string id)
        {
           return  await GetMarqueeAsync(new NicoVideoGetMarqueeQueryInfo(id)).ConfigureAwait(false);
        }

        /// <summary>
        /// Marquee取得
        /// </summary>
        /// <param name="queryInfo"></param>
        /// <returns></returns>
        internal async Task<NicoVideoMarqueeDataResponse> GetMarqueeAsync(NicoVideoGetMarqueeQueryInfo queryInfo)
        {
            using (var api = new NiconicoInfoGetApiXmlImpl(NicoVideoConstant.FLAPI_NICOVIDEO_URL,CreateNotLoginApiHttpClient()))
            {
                return NicoVideoMarqueeDataResponse.Parse(( await api.GetAsync(queryInfo).ConfigureAwait(false)).Content);
            }
        }

        /// <summary>
        /// 非同期にFlv情報取得
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<NicoVideoGetFlvResponse> GetFlvAsync(string id)
        {
            return await GetFlvAsync(new NicoVideoGetFlvQueryInfo(id)).ConfigureAwait(false);
        }


        /// <summary>
        /// 非同期にFlv情報取得
        /// </summary>
        /// <param name="queryInfo"></param>
        /// <returns></returns>
        internal async Task<NicoVideoGetFlvResponse> GetFlvAsync(NicoVideoGetFlvQueryInfo queryInfo)
        {
            using (var flvApi = new NiconicoInfoGetApiQueryStringsImpl(NicoVideoConstant.FLAPI_NICOVIDEO_URL,CreateMostLoginApiHttpClient()))
            {
                return NicoVideoGetFlvResponse.Parse( (await flvApi.GetAsync(queryInfo).ConfigureAwait(false)).Content);


            }
        }





    }
}
